package cn.ldj.ztomcat.classloader;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.io.FileUtil;

public class WebappClassLoader extends URLClassLoader {

    public WebappClassLoader(String docBase, ClassLoader commonClassLoader) {
        super(new URL[]{}, commonClassLoader);

        try {
            File webinfFolder = new File(docBase, "WEB-INF");
            File classesFolder = new File(webinfFolder, "classes");
            File libFolder = new File(webinfFolder, "lib");
            URL url;
            url = new URL("file:" + classesFolder.getAbsolutePath() + "/");
            this.addURL(url);
            System.out.println("加载web应用的servlet完成。拿到了servlet对应的URL="+url);
            List<File> jarFiles = FileUtil.loopFiles(libFolder);
            int countLib = 0;
            for (File file : jarFiles) {
                url = new URL("file:" + file.getAbsolutePath());
                this.addURL(url);
                System.out.println("获取依赖包" + file.getName() + "and URL=" + url);
                countLib++;
            }
            if (0 != countLib) {
                System.out.println("加载web应用的依赖包完成。拿到了依赖包对应的URL。数目=" + countLib);
            }

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void stop() {
        try {
            close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
